//-----------------------------------------------------------------------
// <copyright file="ArticleSubCatDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ArticleSubCatDAOBase
    {
        #region Common methods
        public virtual ArticleSubCat CreateArticleSubCatFromReader(IDataReader reader)
        {
            ArticleSubCat item = new ArticleSubCat();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SubCategoryID"))) item.SubCategoryID = (long)reader["SubCategoryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("NameEn"))) item.NameEn = (string)reader["NameEn"];
                if (!reader.IsDBNull(reader.GetOrdinal("Image"))) item.Image = (string)reader["Image"];
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) item.Summary = (string)reader["Summary"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (int)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) item.CategoryID = (long)reader["CategoryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Desc"))) item.Desc = (string)reader["Desc"];
                if (!reader.IsDBNull(reader.GetOrdinal("Parrentid"))) item.Parrentid = (long)reader["Parrentid"];
                if (!reader.IsDBNull(reader.GetOrdinal("SubcategoryLevel"))) item.SubcategoryLevel = (int)reader["SubcategoryLevel"];
                if (!reader.IsDBNull(reader.GetOrdinal("ImageThumnail"))) item.ImageThumnail = (string)reader["ImageThumnail"];
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) item.Code = (string)reader["Code"];
                if (!reader.IsDBNull(reader.GetOrdinal("MenuRight"))) item.MenuRight = (bool)reader["MenuRight"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (long)reader["UserId"];
                if (!reader.IsDBNull(reader.GetOrdinal("SummaryEn"))) item.SummaryEn = (string)reader["SummaryEn"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateArticleSubCatFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateArticleSubCat methods
            
        public virtual void CreateArticleSubCat(ArticleSubCat articleSubCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleSubCatInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, articleSubCat.Name);
                database.AddInParameter(dbCommand, "@NameEn", DbType.String, articleSubCat.NameEn);
                database.AddInParameter(dbCommand, "@Image", DbType.String, articleSubCat.Image);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, articleSubCat.Summary);
                database.AddInParameter(dbCommand, "@Order", DbType.Int32, articleSubCat.Order);
                database.AddInParameter(dbCommand, "@CategoryID", DbType.Int64, articleSubCat.CategoryID);
                database.AddInParameter(dbCommand, "@Desc", DbType.String, articleSubCat.Desc);
                database.AddInParameter(dbCommand, "@Parrentid", DbType.Int64, articleSubCat.Parrentid);
                database.AddInParameter(dbCommand, "@SubcategoryLevel", DbType.Int32, articleSubCat.SubcategoryLevel);
                database.AddInParameter(dbCommand, "@ImageThumnail", DbType.String, articleSubCat.ImageThumnail);
                database.AddInParameter(dbCommand, "@Code", DbType.String, articleSubCat.Code);
                database.AddInParameter(dbCommand, "@MenuRight", DbType.Boolean, articleSubCat.MenuRight);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, articleSubCat.UserId);
                database.AddInParameter(dbCommand, "@SummaryEn", DbType.String, articleSubCat.SummaryEn);
                database.AddOutParameter(dbCommand, "@SubCategoryID", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                articleSubCat.SubCategoryID = (long)database.GetParameterValue(dbCommand, "@SubCategoryID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateArticleSubCatException, ex);
            }
        }

        #endregion

        #region UpdateArticleSubCat methods
        
        public virtual void UpdateArticleSubCat(ArticleSubCat articleSubCat)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleSubCatUpdate");            
                
                database.AddInParameter(dbCommand, "@SubCategoryID", DbType.Int64, articleSubCat.SubCategoryID);
                database.AddInParameter(dbCommand, "@Name", DbType.String, articleSubCat.Name);
                database.AddInParameter(dbCommand, "@NameEn", DbType.String, articleSubCat.NameEn);
                database.AddInParameter(dbCommand, "@Image", DbType.String, articleSubCat.Image);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, articleSubCat.Summary);
                database.AddInParameter(dbCommand, "@Order", DbType.Int32, articleSubCat.Order);
                database.AddInParameter(dbCommand, "@CategoryID", DbType.Int64, articleSubCat.CategoryID);
                database.AddInParameter(dbCommand, "@Desc", DbType.String, articleSubCat.Desc);
                database.AddInParameter(dbCommand, "@Parrentid", DbType.Int64, articleSubCat.Parrentid);
                database.AddInParameter(dbCommand, "@SubcategoryLevel", DbType.Int32, articleSubCat.SubcategoryLevel);
                database.AddInParameter(dbCommand, "@ImageThumnail", DbType.String, articleSubCat.ImageThumnail);
                database.AddInParameter(dbCommand, "@Code", DbType.String, articleSubCat.Code);
                database.AddInParameter(dbCommand, "@MenuRight", DbType.Boolean, articleSubCat.MenuRight);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int64, articleSubCat.UserId);
                database.AddInParameter(dbCommand, "@SummaryEn", DbType.String, articleSubCat.SummaryEn);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateArticleSubCatException, ex);
            }
        }
        
        #endregion

        #region DeleteArticleSubCat methods
        public virtual void DeleteArticleSubCat(long subCategoryID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleSubCatDelete");
                
                database.AddInParameter(dbCommand, "@SubCategoryID", DbType.Int64, subCategoryID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteArticleSubCatException, ex);
            }
        }

        #endregion

        #region GetArticleSubCat methods
        
        public virtual ArticleSubCat GetArticleSubCat(long subCategoryID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleSubCatGet");
                
                database.AddInParameter(dbCommand, "@SubCategoryID", DbType.Int64, subCategoryID);
                
                ArticleSubCat articleSubCat =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        articleSubCat = CreateArticleSubCatFromReader(reader);
                        reader.Close();
                    }
                }
                return articleSubCat;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleSubCatException, ex);
            }
        }
        
        #endregion

        #region GetArticleSubCatList methods
        public virtual ArticleSubCatCollection GetArticleSubCatList(ArticleSubCatColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleSubCatGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ArticleSubCatCollection articleSubCatCollection = new ArticleSubCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ArticleSubCat articleSubCat = CreateArticleSubCatFromReader(reader);
                        articleSubCatCollection.Add(articleSubCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return articleSubCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleSubCatListException, ex);
            }
        }
        
        public virtual ArticleSubCatCollection GetArticleSubCatList(ArticleSubCatColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetArticleSubCatList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetArticleSubCatList methods
        public virtual ArticleSubCatCollection GetArticleSubCatListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleSubCatGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ArticleSubCatCollection articleSubCatCollection = new ArticleSubCatCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ArticleSubCat articleSubCat = CreateArticleSubCatFromReader(reader);
                        articleSubCatCollection.Add(articleSubCat);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return articleSubCatCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleSubCatListException, ex);
            }
        }
        
        public virtual ArticleSubCatCollection GetArticleSubCatListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetArticleSubCatListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}